package com.sunrise.generico.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class ReportUtil {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void gerarRelatorioJasper(HashMap parametros, JRBeanCollectionDataSource lista, String jasperPath) {

		try {
			FacesContext facesContext = FacesContext.getCurrentInstance();

			ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();

			JasperPrint jasperPrint;
			jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(jasperPath), parametros, lista);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			JRPdfExporter exporter = new JRPdfExporter();

			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

			exporter.exportReport();

			byte[] bytes = baos.toByteArray();

			if (bytes != null && bytes.length > 0) {

				HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

				response.setContentType("application/pdf");

				response.setHeader("Content-disposition", "attachment; filename=\"relatorio.pdf\"");

				response.setContentLength(bytes.length);

				ServletOutputStream outputStream = response.getOutputStream();

				outputStream.write(bytes, 0, bytes.length);

				outputStream.flush();

				outputStream.close();

			}
		} catch (JRException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static StreamedContent gerarRelatorio(String localRelatorio, Map<String, Object> parametros,JRBeanCollectionDataSource lista,
			String nomeArquivoSaida) throws MalformedURLException {

		InputStream relatorio = null;
		ByteArrayOutputStream relat = new ByteArrayOutputStream();
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		ServletContext contextS = (ServletContext) externalContext.getContext();
		String arquivo = contextS.getRealPath(localRelatorio);

		try {

			JasperPrint print = JasperFillManager.fillReport(new FileInputStream(new File(arquivo)), parametros,
					lista);
			JRExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, relat);
			exporter.exportReport();
			relatorio = new ByteArrayInputStream(relat.toByteArray());

		} catch (JRException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.getMessage();
		}

		return new DefaultStreamedContent(relatorio, "application/pdf", nomeArquivoSaida + ".pdf");

	}

}
